<?php

/**
 * @package crontab
 *
 * @author DE-0095
 */

/**
 * tools voor het werken met crontab
 */
class Crontab{
    private $output;

    function __construct(){
        exec("crontab -l",$this->output);
    }

    /**
     * values returns an array with jobs, every job is an array
     * @return Array
     */
    function values(){
        $jobarray= array();
        $nr_jobs = count($this->output);
        foreach ($this->output as $job){
            $pieces = explode("\t",$job);
            array_push($jobarray,$pieces);
        }
        return $jobarray;
    }

    /**
     * Returns a list of all cronjobs that will run today
     * @return Array
     */

    function jobsRunningToday(){
        $jobs = $this->values();
        $today = array();
        $vandaag_dag = date('j');
        $vandaag_maand = date('n');
        $date = date('N');
        if ($date == 7){$vandaag_weekdag= 0;}
        else {$vandaag_weekdag= $date;}

        foreach ($jobs as $job){
            if( $job[2] == $vandaag_dag || $job[2] == "*" &&
                $job[3] == $vandaag_maand || $job[3] == "*" &&
                $job[4] == $vandaag_weekdag || $job[4]=="*"){
                array_push($today,$job[5]);
            }
        }
        return $today;
        }

        
     function wouldJobRun($date,$settings = array('*','1','1','1','*','*')){
         //dateformat date('j-n-Y H:m')
         $dateparts = explode(' ',$date);
         $dayly = explode('-',$dateparts[0]);
         $houry = explode(':',$dateparts[1]);
         $day = $dayly[2];
         $month= $dayly[1];
         $year = $dayly[0];
         $weekday = date("N",mktime(0,0,0,$month,$day,$year));
         if ($weekday == 7){$weekday = 0;}
         unset($dayly);
         $hour = $hourly[0];
         $minutes = $hourly[1];
         unset($hourly);
         if($settings[2]== '*' || $settings[2] == $day &&
            $settings[3]== '*' || $settings[3] == $month &&
            $settings[4]== '*' || $settings[4] == $year &&
            $settings[5]== '*' || $settings[5] == $weekday){
             return true;
         }
         else{return false;}
     }

     function jobOverview(){
         $month = date('m');
         $year = date('Y');
         $fistDayOfMonth = 1;
         $last = strtotime('+1 month -1 day', mktime(0,0,0,$year,$month,1));
         $lastDayOfMonth = date('d',$last);
         for ($i = 1;$i <= $last; $i++){
         $dayToCheck = date("N",mktime(0,0,0,$month,$day,$year));
         }

     }
}

?>
